﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using BenefitHelper.Sys;
using System.Data.Entity;
namespace BenefitHelper.Data.Day
{
    /// <summary>
    /// 账户资金情况
    /// </summary>
    public class AccountDayMoney
    {
        /// <summary>
        /// 编号
        /// </summary>
        public int Id { get; set; }
        /// <summary>
        /// 账户
        /// </summary>
        public int AccountId { get; set; }
        /// <summary>
        /// 账户
        /// </summary>
        public virtual Account Account { get; set; }
        /// <summary>
        /// 日期时间
        /// </summary>
        public double Datetime { get; set; }
        /// <summary>
        /// 多仓持仓
        /// </summary>
        public int LongPosition { get; set; }
        /// <summary>
        /// 空仓持仓
        /// </summary>
        public int ShortPosition { get; set; }
        /// <summary>
        /// 期初结存
        /// </summary>
        public double PresetTlePosit { get; set; }
        /// <summary>
        /// 期末结存
        /// </summary>
        public double SettlePosit { get; set; }
        /// <summary>
        /// 冻结保证金
        /// </summary>
        public double FrozenMargin { get; set; }
        /// <summary>
        /// 冻结手续费
        /// </summary>
        public double FrozenCommission { get; set; }
        /// <summary>
        /// 入金金额
        /// </summary>
        public double Deposit { get; set; }
        /// <summary>
        /// 出金金额
        /// </summary>
        public double WithDraw { get; set; }
        /// <summary>
        /// 交割保证金
        /// </summary>
        public double DeliveryMargin { get; set; }
        /// <summary>
        /// 交割手续费
        /// </summary>
        public double DeliveryCommission { get; set; }
        /// <summary>
        /// 追加保证金
        /// </summary>
        public double AppendMargin { get; set; }
        /// <summary>
        /// 基础保证金
        /// </summary>
        public double BaseMargin { get; set; }
        /// <summary>
        /// 质押金额
        /// </summary>
        public double Mortgage { get; set; }
        /// <summary>
        /// 上次质押金额
        /// </summary>
        public double Premortgage { get; set; }
        /// <summary>
        /// 手续费
        /// </summary>
        public double Commission { get; set; }
        /// <summary>
        /// 可用资金
        /// </summary>
        public double AvailableMoney { get; set; }
        /// <summary>
        /// 可取资金
        /// </summary>
        public double WithDrawQuota { get; set; }
        /// <summary>
        /// 动态权益
        /// </summary>
        public double DynamicMoney { get; set; }
        /// <summary>
        /// 占用保证金
        /// </summary>
        public double Margin { get; set; }
        /// <summary>
        /// 平仓盈亏
        /// </summary>
        public double CloseProfit { get; set; }
        /// <summary>
        /// 持仓盈亏
        /// </summary>
        public double PositionProfit { get; set; }
        /// <summary>
        /// 风险度
        /// </summary>
        public double RiskRate { get; set; }
        /// <summary>
        /// 交易日
        /// </summary>
        public int TradeHistoryId { get; set; }
        /// <summary>
        /// 交易日
        /// </summary>
        public virtual TradeHistory TradeHistory { get; set; }


        /// <summary>
        /// 删除当日所有记录
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        public void Delete(int tradeHistoryId, DB.DBManager db)
        {
            var query = from t in db.AccountDayMoney where t.TradeHistoryId == tradeHistoryId select t;
            foreach (Data.Day.AccountDayMoney c in query)
            {
                db.AccountDayMoney.Remove(c);
            }
            db.SaveChanges();
        }

        /// <summary>
        /// 保存账号日资金情况
        /// </summary>
        /// <param name="tradeHistoryId">插入时间编号</param>
        /// <param name="pdate">插入时间</param>
        public void InitDayAccountDayMoney(int tradeHistoryId, string pdate, DB.DBManager db)
        {
            List<Data.ServerData.T_Account> serverAccounts = new List<ServerData.T_Account>();
            int connectionStringCount = System.Configuration.ConfigurationManager.ConnectionStrings.Count;
            for (int i = 0; i < connectionStringCount; i++)
            {
                string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings[i].ConnectionString;
                if (System.Configuration.ConfigurationManager.ConnectionStrings[i].ProviderName.Equals("System.Data.OracleClient"))
                {
                    Common.ADO.Tools.OracleHelper.connectionString = connectionString;
                    List<BenefitHelper.Data.ServerData.T_Account> accountList = new BenefitHelper.Data.ServerData.T_Account().GetList();
                    foreach (BenefitHelper.Data.ServerData.T_Account _account in accountList)
                    {
                        serverAccounts.Add(_account);
                    }
                }
            }
            foreach (Data.ServerData.T_Account account in serverAccounts)
            {
                Data.Day.AccountDayMoney change = new Data.Day.AccountDayMoney();
                Sys.Account _account = new Sys.Account();
                change.AccountId = _account.GetAccountId(account.LoginAccount);
                Data.ServerData.T_SettlementMoney money = new ServerData.T_SettlementMoney().GetSettlementMoney(account.Id, pdate, db);
                change.AppendMargin = money.AppendMargin;
                change.AvailableMoney = money.AvailableMoney;
                change.BaseMargin = money.BaseMargin;
                change.CloseProfit = money.CloseProfit;
                change.Commission = money.Commission;
                change.Datetime = money.Datetime;
                change.DeliveryCommission = money.DeliveryCommission;
                change.DeliveryMargin = money.DeliveryMargin;
                change.Deposit = money.Deposit;
                change.DynamicMoney = money.DynamicMoney;
                change.FrozenCommission = money.FrozenCommission;
                change.FrozenMargin = money.FrozenMargin;
                change.LongPosition = money.LongPosition;
                change.Margin = money.Margin;
                change.Mortgage = money.Mortgage;
                change.PositionProfit = money.PositionProfit;
                change.Premortgage = money.Premortgage;
                change.PresetTlePosit = money.PresetTlePosit;
                change.RiskRate = money.RiskRate;
                change.SettlePosit = money.SettlePosit;
                change.ShortPosition = money.ShortPosition;
                change.TradeHistoryId = tradeHistoryId;
                change.WithDraw = money.WithDraw;
                change.WithDrawQuota = money.WithDrawQuota;
                db.AccountDayMoney.Add(change);
            }
            db.SaveChanges();
        }


        public AccountDayMoney GetAccountDayMoney(int tradeHistoryId, int accountid, DB.DBManager db)
        {
            AccountDayMoney returnValue = new AccountDayMoney();
            var query = db.AccountDayMoney.Where(a => a.AccountId == accountid).Where(a => a.TradeHistoryId == tradeHistoryId).Include(a => a.Account);
            if (query.Count() > 0)
            {
                returnValue = query.First();
            }
            return returnValue;
        }
    }
}